package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.*;
import com.leoman.service.*;
import com.leoman.utils.DateUtils;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 普通用户管理
 * Created by 涂奕恒 on 2016/10/21.
 */
@Controller
@RequestMapping(value = "backend/user")
public class UserController extends CommonController {

    @Autowired
    private UserService userService;

    @Autowired
    private ThirdUserService thirdUserService;

    @Autowired
    private UserTagService userTagService;

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private UserSealInfoService userSealInfoService;

    @RequestMapping("index")
    public String index() {
        return "backend/用户列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String nickName,
                     Integer type,
                     String startTime,
                     String endTime,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<User> page = userService.pageByParams(nickName, type, startTime, endTime, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer userId) {
        if (null != userId) {
            User user = userService.getById(userId);

            // 查询用户第三方账号信息
            ThirdUser wxThird = thirdUserService.findOneByUserIdAndType(userId, Constant.USER_TYPE_WEIXIN);
            if (null != wxThird) {
                model.addAttribute("wxNum", wxThird.getNickname());
            }
            ThirdUser wbThird = thirdUserService.findOneByUserIdAndType(userId, Constant.USER_TYPE_WEIBO);
            if (null != wbThird) {
                model.addAttribute("wbNum", wbThird.getNickname());
            }
            ThirdUser qqThird = thirdUserService.findOneByUserIdAndType(userId, Constant.USER_TYPE_QQ);
            if (null != qqThird) {
                model.addAttribute("qqNum", qqThird.getNickname());
            }

            model.addAttribute("user", user);

            // 查询用户标签
            List<UserTag> userTagList = userTagService.findListByUserId(userId);
            StringBuffer sb = new StringBuffer("");
            for (UserTag userTag : userTagList) {
                sb.append(userTag.getContent()).append("、");
            }

            if (sb.length() > 0) {
                model.addAttribute("tags", sb.substring(0, sb.length() - 1));
            } else {
                model.addAttribute("tags", sb);
            }

            // 查询用户收信地址列表
            List<UserAddress> userAddressList = userAddressService.findListByUserId(userId);
            model.addAttribute("addressList", userAddressList);

            // 禁用记录列表
            List<UserSealInfo> userSealInfoList = userSealInfoService.findListByUserId(userId);
            for (UserSealInfo userSealInfo : userSealInfoList) {
                userSealInfo.setCreateTimeStr(DateUtils.longToStringWithFormat(userSealInfo.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));
            }
            model.addAttribute("sealInfoList", userSealInfoList);
        }

        return "backend/用户详情";
    }

    /**
     * 启用 or 禁用APP用户
     *
     * @param userId  用户ID
     * @param status  状态
     * @param content 变更记录原因
     */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Integer changeStatus(Integer userId, Integer status, String content) {
        try {
            Integer[] userIds = new Integer[1];
            userIds[0] = userId;
            return userService.changeUserStatus(userIds, status, content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
